﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using InHD.Properties;

namespace InHD.model
{
    class RegisterPrinting: IDataErrorInfo
    {
        #region Property
        public string InvoiceUNK { get; set; }
        public string Status { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime ExpireDate { get; set; }
        public DateTime Modified { get; set; }
        public int ModifiedBy { get; set; }
        public DateTime RegisterDate { get; set; }
        public string ReceiverUNK { get; set; }
        public int Number { get; set; }
        public string RegUNK { get; set; }
        #endregion

        #region Creation
        protected RegisterPrinting()
        {
        }

        public static RegisterPrinting CreateNewRegisterPrinting()
        {
            RegisterPrinting registerPrinting = new RegisterPrinting();
            registerPrinting.StartDate = DateTime.Now;
            registerPrinting.ExpireDate = DateTime.Now;
            return registerPrinting;
        }

        public static RegisterPrinting CreateNewRegisterPrinting(string invoiceUNK, DateTime startDate, DateTime expireDate, int number, string receiverUNK, DateTime regDate, DateTime modified, int modifiedBy, string status, string regUNK)
        {
            return new RegisterPrinting()
            {
                StartDate = startDate,
                ExpireDate = expireDate,
                Number = number,
                ReceiverUNK = receiverUNK,
                RegisterDate = regDate,
                ModifiedBy = modifiedBy,
                Modified = modified,
                Status = status,
                InvoiceUNK = invoiceUNK,
                RegUNK = regUNK
            };
        }

        #endregion

        #region IDataErrorInfo Members

        string IDataErrorInfo.Error { get { return null; } }

        string IDataErrorInfo.this[string propertyName]
        {
            get { return this.GetValidationError(propertyName); }
        }

        #endregion // IDataErrorInfo Members

        #region Validation Methods

        /// <summary>
        /// Returns true if this object has no validation errors.
        /// </summary>
        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                    if (GetValidationError(property) != null)
                        return false;

                return true;
            }
        }

        static readonly string[] ValidatedProperties = 
        { 
            "StartDate", 
            "InvoiceUNK", 
            "ExpireDate",
            "ReceiverUNK",
            "Number"
        };

        string GetValidationError(string propertyName)
        {
            if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;

            string error = null;

            switch (propertyName)
            {
                case "StartDate":
                    error = this.ValidateStartDate();
                    break;

                case "InvoiceUNK":
                    error = this.ValidateInvoiceTemplateID();
                    break;

                case "ExpireDate":
                    error = this.ValidateExpireDate();
                    break;

                case "ReceiverUNK":
                    error = this.ValidateReceiverID();
                    break;

                case "Number":
                    error = this.ValidateNumber();
                    break;

                default:
                    Debug.Fail("Unexpected property being validated on CompanyInfo: " + propertyName);
                    break;
            }

            return error;
        }

        private string ValidateStartDate()
        {
            if (DateTime.Compare(this.StartDate, this.ExpireDate) >= 0)
            {
                return Resources.RegPrint_DateCompare;
            }
            return null;
        }

        private string ValidateInvoiceTemplateID()
        {
            if (this.InvoiceUNK == null)
            {
                return Resources.RegPrint_SelectInvTemplate;
            }
            return null;
        }

        private string ValidateExpireDate()
        {
            if (DateTime.Compare(this.StartDate, this.ExpireDate) >= 0)
            {
                return Resources.RegPrint_DateCompare;
            }

            return null;
        }

        private string ValidateReceiverID()
        {
            if (string.IsNullOrEmpty(this.ReceiverUNK))
            {
                return Resources.RegPrint_SelectReceiver;
            }
            return null;
        }

        private string ValidateNumber()
        {
            if (this.Number <= 0)
            {
                return Resources.RegPrint_GreaterThanZero;
            }
            return null;
        }

        #endregion

        public string InvoiceName { get; set; }
    }

    ////Helper Class
    class RegisterInfo
    {
        public string ComputerName { get; set; }
        public string ComputerCode { get; set; }
        public string OperatingName { get; set; }
        public string CPUName { get; set; }
        public string RamInfo { get; set; }
        public string RequireInfo { get; set; }
        public string PrinterInfo { get; set; }
        public string PrinterProducer { get; set; }
        public string PrinterRequireInfo { get; set; }

        public RegisterInfo()
        {
        }

        public RegisterInfo(RegisterInfo info)
        {
            this.ComputerName = info.ComputerName;
            this.ComputerCode = info.ComputerCode;
            OperatingName = info.OperatingName;
            CPUName = info.CPUName;
            RamInfo = info.RamInfo;
            RequireInfo = info.RequireInfo;
            PrinterInfo = info.PrinterInfo;
            PrinterProducer = info.PrinterProducer;
            PrinterRequireInfo = info.PrinterRequireInfo;
        }

        public RegisterInfo(string cName, string cCode, string oName, string cpu, string rInfo, string cRequire, string pInfo, string pProducer, string pRequire)
        {
            ComputerName = cName;
            ComputerCode = cCode;
            OperatingName = oName;
            CPUName = cpu;
            RamInfo = rInfo;
            RequireInfo = cRequire;
            PrinterInfo = pInfo;
            PrinterProducer = pProducer;
            PrinterRequireInfo = pRequire;
        }
    }
}
